package com.tc.neo4j.repository;

import com.tc.neo4j.entity.FriendshipRelation;
import com.tc.neo4j.entity.StudentNode;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface StudentRepository extends Neo4jRepository<StudentNode,Long> {

    /**
     * 通过name查找学生node
     * @param name
     * @return
     */
    StudentNode findByName(String name);

    /**
     * 根据name获取学生 in 友谊关系
     * @param name
     * @return
     */
    @Query("match " +
            "p=(a:StudentNode) - [r:FRIENDSHIP_RELATION*0..] -> " +
            "(b:StudentNode) " +
            "where b.name = $name " +
            "return p")
    List<FriendshipRelation> inFriendship(@Param("name")String name);

    /**
     * 根据name获取学生 out 友谊关系
     * @param name
     * @return
     */
    @Query("match " +
            "p=(b:StudentNode) " +
            "- [rr:FRIENDSHIP_RELATION*0..] -> (c:StudentNode) " +
            "where b.name = $name " +
            "return p")
    List<FriendshipRelation> outFriendship(@Param("name")String name);

    /**
     * 根据name获取学生 both 友谊关系
     * @param name
     * @return
     */
    @Query("match " +
            "p=(a:StudentNode) <- [r:FRIENDSHIP_RELATION*0..] -> " +
            "(b:StudentNode) " +
            "<- [rr:FRIENDSHIP_RELATION*0..] -> (c:StudentNode) " +
            "where b.name = $name " +
            "return p")
    List<FriendshipRelation> bothFriendship(@Param("name")String name);
}